package agentV1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hc_server/pkg/app"
	"hc_server/pkg/util"
	"hc_server/routers/service/agentService"
	"hc_server/routers/service/codeService"
	"hc_server/runtime/e"
)

type AgentLoginForm struct {
	Mobile   interface{} //手机号
	Password string      //密码 md5
}

// AgentLogin 代理商登录
func AgentLogin(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		form    AgentLoginForm
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &form)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	if form.Mobile == "" || form.Password == "" {
		appG.ResponseError("账号或密码不能为空", nil)
		return
	}

	agent := agentService.Agent{
		Mobile:   cast.ToString(form.Mobile),
		Password: form.Password,
	}

	result, err := agent.AgentLogin()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	token, err := util.GenerateToken(cast.ToString(agent.Mobile), agent.Password, result["id"])
	if err != nil {
		appG.ResponseError("token获取失败", nil)
		return
	}

	appG.ResponseLoginSuccess("登录成功", result, token)
	return
}

type AgentRegisterForm struct {
	RecommenderCode interface{} `json:"recommenderCode"` //推荐码 代理商编码
	Name            string      `json:"name"`            //代理商名称
	Mobile          interface{} `json:"mobile"`          //手机号
	Password        string      `json:"password"`        //密码 md5
	Code            string      `json:"codeService"`     //短信验证码
}

// AgentRegister 代理商注册
func AgentRegister(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		form    AgentRegisterForm
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &form)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	if form.Mobile == "" || form.Password == "" {
		appG.ResponseError("账号或密码不能为空", nil)
		return
	}

	code := codeService.Code{
		Code:   form.Code,
		Mobile: form.Mobile,
	}

	//验证手机验证码是否正确
	exist, err := code.ExistCodeByMobile()
	if err != nil || !exist {
		appG.ResponseError(err.Error(), nil)
		return
	}

	recommenderCode := cast.ToString(form.RecommenderCode)
	if recommenderCode == "" || len(recommenderCode) < 7 {
		recommenderCode = "P000000"
	}

	if len(recommenderCode) > 0 && recommenderCode[0] == 'P' {
		recommenderCode = recommenderCode[1:] // 去掉开头的 "P"
	}

	agent := agentService.Agent{
		Mobile:          cast.ToString(form.Mobile),
		Password:        form.Password,
		RecommenderCode: recommenderCode,
		Name:            form.Name,
	}

	err = agent.AgentRegister()
	code.UpdateCodeIsCheck()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("注册成功,请返回登录", nil)
}

type AgentMerchantAgentListParam struct {
	Id       any    `json:"id"`
	PageNum  any    `json:"pageNum"`
	PageSize any    `json:"pageSize"`
	Keyword  string `json:"keyword"`
}

// AgentMerchantAgentList 代理商列表
func AgentMerchantAgentList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   AgentMerchantAgentListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	if cast.ToInt(param.Id) == 0 {
		appG.ResponseError("缺少参数id", nil)
		return
	}

	agent := agentService.Agent{
		Id:       cast.ToInt(param.Id),
		PageNum:  cast.ToInt(param.PageNum),
		PageSize: cast.ToInt(param.PageSize),
		Keyword:  param.Keyword,
	}

	agents, err := agent.AgentMerchantAgentList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("代理商列表", agents)
}

type SpecialIndustryMerchantListParam struct {
	PageNum  any    `json:"pageNum"`
	PageSize any    `json:"pageSize"`
	Keyword  string `json:"keyword,omitempty"`
}

// SpecialIndustryMerchantList 特殊行业商户列表
func SpecialIndustryMerchantList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   SpecialIndustryMerchantListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	agent := agentService.Agent{
		Id:       cast.ToInt(userId),
		PageNum:  cast.ToInt(param.PageNum),
		PageSize: cast.ToInt(param.PageSize),
		Keyword:  param.Keyword,
	}

	result := agent.SpecialIndustryMerchantList()

	appG.ResponseSuccess("成功", result)
}

type AgentHomeParam struct {
	Type any `json:"type"` //1=当日 2=昨日 3=本月
}

func AgentHome(c *gin.Context) {
	appG := app.Gin{C: c}

	var (
		param   AgentHomeParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	userId, _ := c.Get("userId")

	agent := agentService.Agent{
		Id:       cast.ToInt(userId),
		TimeType: cast.ToInt(param.Type),
	}

	result, err := agent.AgentHome()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type AgentDataParam struct {
	InfoType     any `json:"infoType"`     //1=今日 2=昨日 3=本月 4=上月 5=历史
	AnalysisType any `json:"analysisType"` //1=昨日 2=上周 3=上月
	DynamicType  any `json:"dynamicType"`  //1=昨日 2=上周 3=上月
}

func AgentData(c *gin.Context) {
	appG := app.Gin{C: c}

	userId, _ := c.Get("userId")

	agent := agentService.Agent{
		Id: cast.ToInt(userId),
	}

	result, err := agent.AgentData()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type AgentBindSettlementCardParam struct {
	BankNo any `json:"bankNo"`
	BankId any `json:"bankId"`
}

// AgentBindSettlementCard 绑定结算银行卡
func AgentBindSettlementCard(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   AgentBindSettlementCardParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}
	userId, _ := c.Get("userId")

	agent := agentService.Agent{
		Id:     cast.ToInt(userId),
		BankNo: cast.ToString(param.BankNo),
		BankId: cast.ToInt(param.BankId),
	}

	err := agent.AgentBindSettlementCard()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", nil)
}
